/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package session;

import entities.BookRooms;
import entities.Customers;
import entities.Restaurants;
import entities.Rooms;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

/**
 *
 * @author TANGQUOCTUAN
 */
@Stateless
public class BookRoomsFacade extends AbstractFacade<BookRooms> implements BookRoomsFacadeLocal {

    @PersistenceContext(unitName = "eProject_Tunga_Restaurant-ejbPU")
    private EntityManager em;

    protected EntityManager getEntityManager() {
        return em;
    }

    public BookRoomsFacade() {
        super(BookRooms.class);
    }

    public List showBookRoom() {
        Query r = em.createNamedQuery("BookRooms.findAll");
        return r.getResultList();

    }

    public boolean deleteBookRoom(Integer id) {
        BookRooms br = em.find(BookRooms.class, id);
        em.remove(br);
        return true;
    }

    public boolean updateRoom(Integer id, boolean status) {
        BookRooms br = em.find(BookRooms.class, id);
        br.setStatus(status);
        em.persist(br);
        return true;
    }

    public boolean insertBookRoom(int roomID, int resID, String custID, int capacity, Date startDayTime, Date endDayTime, BigDecimal reserveFee) {
        Rooms r = em.find(Rooms.class, roomID);
        Restaurants rs = em.find(Restaurants.class, resID);
        Customers c = em.find(Customers.class, custID);
        BookRooms br = new BookRooms(capacity, startDayTime, endDayTime, reserveFee);
        br.setRooms(r);
        br.setRestaurants(rs);
        br.setCustomers(c);
        r.getBookRoomsList().add(br);
        rs.getBookRoomsList().add(br);
        c.getBookRoomsList().add(br);
        em.persist(br);
        return true;
    }

    public List<BookRooms> showallbyID(String username) {
        List<BookRooms> list = new ArrayList<BookRooms>();
        Query r = em.createNamedQuery("BookRooms.findByBookRoomByUser");
        r.setParameter("custID", username);
        return list = r.getResultList();
    }

    public List<BookRooms> reportShowBookRoom(Date fromdate, Date todate) {
        List<BookRooms> list = new ArrayList<BookRooms>();
        Query r = em.createNamedQuery("BookRooms.findReportBookRoom");
        r.setParameter("fromdate", fromdate);
        r.setParameter("todate", todate);
        return list = r.getResultList();
    }
}
